package com.ljkj.zjf.rest;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ljkj.access.Tuser;
import com.ljkj.common.application.SpringApplicationContext;
import com.ljkj.common.db.domain.ZjfOrder;
import com.ljkj.common.db.util.JSONUTIL;
import com.ljkj.zjf.common.CacheManager;
import com.ljkj.zjf.common.GlobalNames;
import com.ljkj.zjf.service.TuserService;
import com.ljkj.zjf.service.ZjfOrderItemService;
import com.ljkj.zjf.service.ZjfOrderService;

@Path("rest/order")
public class ZjfOrderSource {
	
	private static ZjfOrderService zjfOrderService;
	private static TuserService tuserService;
	private static ZjfOrderItemService zjfOrderItemService;
	
	//初始化service
	static {
		zjfOrderService  = (ZjfOrderService) SpringApplicationContext.getBean("zjfOrderService");
		tuserService =  (TuserService) SpringApplicationContext.getBean("tuserService");
		zjfOrderItemService = (ZjfOrderItemService) SpringApplicationContext.getBean("zjfOrderItemService");
	}
	
	private static Logger logger = LoggerFactory.getLogger(ZjfOrderSource.class);
	
	@Path("/list")
	@POST
	@Produces("application/json; charset=utf-8")
	public String orderList(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		String userId = json_object.getString("userId"); 
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String matchRes = CacheManager.getSession().getIfPresent(userId);
		if (null == matchRes || "".equals(matchRes)) {
			errorMap.put("errMsg", "您尚未登录，请登录再操作。");
			return JSON.toJSONString(errorMap);
		}
		
		String params = json_object.getString("params");
		String pageIndex = json_object.getString("pageIndex");
		int currentPage = 1;
		if(pageIndex != null && !"".equals(pageIndex)){
			currentPage = Integer.parseInt(pageIndex)+1;
		}
		String pageSize_str = json_object.getString("pageSize");
		int pageSize = 10;
		if(pageSize_str != null && !"".equals(pageSize_str)){
			pageSize = Integer.parseInt(pageSize_str);
		}
		String sortField = json_object.getString("sortField");
		String sortOrder = json_object.getString("sortOrder");
		
		String result = this.zjfOrderService.getOrdersByProperties(params, currentPage, pageSize, sortField, sortOrder);
		System.out.println(result);
		return result;
		
	}
	
	@Path("/list_from_goods_item")
	@POST
	@Produces("application/json; charset=utf-8")
	public String orderItemsList(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		String params = json_object.getString("params");
		String pageIndex = json_object.getString("pageIndex");
		int currentPage = 1;
		if(pageIndex != null && !"".equals(pageIndex)){
			currentPage = Integer.parseInt(pageIndex)+1;
		}
		String pageSize_str = json_object.getString("pageSize");
		int pageSize = 10;
		if(pageSize_str != null && !"".equals(pageSize_str)){
			pageSize = Integer.parseInt(pageSize_str);
		}
		String sortField = json_object.getString("sortField");
		String sortOrder = json_object.getString("sortOrder");
		
		String orderId = json_object.getString("orderId");
		String result = this.zjfOrderItemService.getOrderItemsByProperties(params, orderId, currentPage, pageSize, sortField, sortOrder);
		System.out.println(result);
		return result;
	}
	
	
	@Path("/info")
	@POST
	@Produces("application/json; charset=utf-8")
	public String addOrEditOrderInfo(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		String userId = json_object.getString("userId"); 
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String orderId = json_object.getString("orderId");
		ZjfOrder zjfOrder = null;
		if(orderId != null && !"".equals(orderId)){
			zjfOrder = this.zjfOrderService.getOrderById(orderId);
		}
		if(zjfOrder == null){
			zjfOrder = new ZjfOrder();
		}
		
		
		String orderStatus_temp = json_object.getString("orderStatus");
		Integer orderStatus = null;
		if(orderStatus_temp != null && !"".equals(orderStatus_temp)){
			orderStatus = Integer.parseInt(orderStatus_temp);
			zjfOrder.setOrderStatus(orderStatus);
		}
		
		String orderIsDelivery_temp = json_object.getString("orderIsDelivery");
		Integer orderIsDelivery = null;
		if(orderIsDelivery_temp != null && !"".equals(orderIsDelivery_temp)){
			orderIsDelivery = Integer.parseInt(orderIsDelivery_temp);
			zjfOrder.setOrderIsDelivery(orderIsDelivery);
		}
		
		String dtName = json_object.getString("dtName");
		if(dtName!= null){
			zjfOrder.setDtName(dtName);
		}
		
		String dtNum = json_object.getString("dtNum");
		if(dtNum!= null){
			zjfOrder.setDtNum(dtNum);
		}
		
		String orderShipName = json_object.getString("orderShipName");
		if(orderShipName!= null){
			zjfOrder.setOrderShipName(orderShipName);
		}
		
		String orderShipArea = json_object.getString("orderShipArea");
		if(orderShipArea!= null){
			zjfOrder.setOrderShipAddr(orderShipArea);
		}
		
		String orderShipAddr = json_object.getString("orderShipAddr");
		if(orderShipAddr!= null){
			zjfOrder.setOrderShipAddr(orderShipAddr);
		}
		
		String orderShipZip = json_object.getString("orderShipZip");
		if(orderShipZip!= null){
			zjfOrder.setOrderShipZip(orderShipZip);
		}
		
		String orderShipTel = json_object.getString("orderShipTel");
		if(orderShipTel!= null){
			zjfOrder.setOrderShipTel(orderShipTel);
		}
		
		String orderShipEmail = json_object.getString("orderShipEmail");
		if(orderShipEmail!= null){
			zjfOrder.setOrderShipEmail(orderShipEmail);
		}
		
		String orderShipTime = json_object.getString("orderShipTime");
		if(orderShipTime!= null){
			zjfOrder.setOrderShipTime(orderShipTime);
		}
		
		String orderShipMobile = json_object.getString("orderShipMobile");
		if(orderShipMobile!= null){
			zjfOrder.setOrderShipMobile(orderShipMobile);
		}
		
		String orderIsTax_temp = json_object.getString("orderIsTax");
		Integer orderIsTax = null;
		if(orderIsTax_temp != null && !"".equals(orderIsTax_temp)){
			orderIsTax = Integer.parseInt(orderIsTax_temp);
			zjfOrder.setOrderIsTax(orderIsTax);
		}
		
		String orderCostTax_temp = json_object.getString("orderCostTax");
		BigDecimal orderCostTax = null;
		if(orderCostTax_temp != null && !"".equals(orderCostTax_temp)){
			orderCostTax = new BigDecimal(orderCostTax_temp);
			zjfOrder.setOrderCostTax(orderCostTax);
		}
		
		String orderTaxCompany = json_object.getString("orderTaxCompany");
		if(orderTaxCompany!= null){
			zjfOrder.setOrderTaxCompany(orderTaxCompany);
		}
		
		String orderDiscount_temp = json_object.getString("orderDiscount");
		BigDecimal orderDiscount = null;
		if(orderDiscount_temp != null && !"".equals(orderDiscount_temp)){
			orderDiscount = new BigDecimal(orderDiscount_temp);
			zjfOrder.setOrderDiscount(orderDiscount);
		}
		
		Integer disabledState = null;
		String disabledState_temp = json_object.getString("disabledState");
		if(disabledState_temp != null && !"".equals(disabledState_temp)){
			disabledState = Integer.parseInt(disabledState_temp);
			zjfOrder.setDisabledState(disabledState);
		}
		
		Tuser tuser = this.tuserService.getUserById(Integer.valueOf(userId));
		
		String result = this.zjfOrderService.addOrEditOrderInfo(zjfOrder, tuser);
		return result;
		
	}


}
